package ar.Location.Api.Foursquare;

import java.util.ArrayList;

import android.util.Log;
import ar.Location.Constant.Define;
import ar.Location.Helper.StringHelper;
import ar.Location.PlaceApi.AbsAPI;
import ar.Location.PlaceObject.Place;
import ar.globals.Globals;
import fi.foyt.foursquare.api.FoursquareApi;
import fi.foyt.foursquare.api.FoursquareApiException;
import fi.foyt.foursquare.api.Result;
import fi.foyt.foursquare.api.entities.CompactVenue;
import fi.foyt.foursquare.api.entities.Location;
import fi.foyt.foursquare.api.entities.VenuesSearchResult;

public class FoursquareAPI extends AbsAPI {

	private static final String TAG = "FoursquareAPI";
	private static FoursquareApi foursquareApi = null;

	public FoursquareAPI() {
		initFoursquareInstance();
	}

	private void initFoursquareInstance() {
		// TODO Auto-generated method stub
		if (foursquareApi == null) {
			foursquareApi = new FoursquareApi(Define.FOURSQUARE_API_CLIENT_ID,
					Define.FOURSQUARE_API_CLIENT_SECRET,
					Define.FOURSQUARE_API_REDIRECT_URL);
		}
	}

	@Override
	public ArrayList<Place> getListPlacesByGeoPoint(String latitude,
			String longitude, int radiusMetters) {
		// TODO Auto-generated method stub
		return getListPlacesByType(latitude, longitude, "", radiusMetters);
	}

	@Override
	public ArrayList<Place> getListPlacesByType(String latitude,
			String longitude, String type, int radiusMetters) {
		// TODO Auto-generated method stub
		try {
			
			if(radiusMetters < 100) {
				radiusMetters = 100;
			} else {
				if(radiusMetters > 2000) {
					radiusMetters = 2000;
				}
			}
			
			Result<VenuesSearchResult> results = foursquareApi.venuesSearch(
					latitude + "," + longitude, type, Globals.MAX_NUMBER_OF_SEARCHED_LOCATIONS, "checkin",
					String.valueOf(radiusMetters), null);

			int code = results.getMeta().getCode();

			if (code == 200) {
				CompactVenue[] listCompactVenues = results.getResult()
						.getVenues();
				if (listCompactVenues != null && listCompactVenues.length > 0) {

					ArrayList<Place> listPlaces = new ArrayList<Place>();

					int nCompactVenues = listCompactVenues.length;
					for (int i = 0; i < nCompactVenues; i++) {
						Place place = getPlace(listCompactVenues[i], type);
						if (place != null) {
							listPlaces.add(place);
						}
					}
					return listPlaces;
				}
			}
			return null;
		} catch (FoursquareApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d(TAG, e.getMessage());
			return null;
		}
	}

	private Place getPlace(CompactVenue compactVenue, String type) {
		// TODO Auto-generated method stub
		try {
			if (compactVenue != null) {
				String name = compactVenue.getName();
				Location location = compactVenue.getLocation();
				String address = location.getAddress();
				String latitude = location.getLat().toString();
				String longitude = location.getLng().toString();
				String imageUrl = null;
				if(compactVenue.getCategories() != null && compactVenue.getCategories().length > 0) {
					imageUrl = compactVenue.getCategories()[0].getIcon();
				}
				String websiteUrl = compactVenue.getUrl();
				String rank = compactVenue.getStats().getCheckinsCount()
						.toString();
				// String distance = location.getDistance().toString();
				String description = "Check in count: " + rank;
				String phoneNumber = compactVenue.getContact().getPhone();

				FoursquarePlace fsPlace = null;
				
				fsPlace = new FoursquarePlace(Define.DS_FOURSQUARE);

				if (name != null) {
					fsPlace.name = name;
				}

				if (address != null) {
					fsPlace.address = address;
				}

				if (latitude != null && longitude != null) {
					fsPlace.latitude = latitude;
					fsPlace.longitude = longitude;
				}

				if(imageUrl != null) {
					fsPlace.imageUrl = StringHelper.edit(imageUrl);
				}
				
				if (websiteUrl != null) {
					fsPlace.websiteUrl = websiteUrl;
				}

				if (rank != null) {
					fsPlace.rank = rank;
				}

				if (description != null) {
					fsPlace.description = description;
				}

				if (phoneNumber != null) {
					fsPlace.phoneNumber = phoneNumber;
				}

				return fsPlace;
			}
			return null;
		} catch (Exception e) {
			// TODO: handle exception
			Log.d(TAG, e.getMessage());
			return null;
		}
	}
}
